package com.hjy.love.config;

import com.alibaba.fastjson.JSON;
import com.hjy.love.utils.SendPostUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Properties;

/**
 * 类说明:
 *
 * @author ****
 * @date 2020-12-08 15:03  新建
 */
@Component
public class TimeCron {

    @Value("${email.username}")
    private String EmailUsername;

    @Value("${email.password}")
    private String EmailPassword;

    @Value("${email.GetMailAddress}")
    private String GetMailAddress;

    @Value("${email.GetSubject}")
    private String GetSubject;

    @Value("${email.SentMailName}")
    private String SentMailName;

    //每天5:21
    //@Scheduled(cron = "0 21 5 * * ?")
    //每隔20秒
    @Scheduled(cron = "*/20 * * * * ?")
    public void timerCron() {
        String s = SendPostUtil.sendGet("https://chp.shadiao.app/api.php", "a=1", null);
        System.out.println(s);
        SendEmail(s);
    }

    /**
     * @return java.util.Properties
     * @Author HuJingYu
     * @Date 15:47 2020-12-08
     * @Description 邮箱配置
     */
    public Properties getProperties() {
        Properties p = new Properties();
        p.setProperty("username", EmailUsername);
        p.setProperty("password", EmailPassword);
        Map<String, Object> map = JSON.parseObject("{\"mail.imap.host\": \"imap.qq.com\", \"mail.imap.port\": \"143\", \"mail.smtp.auth\": \"true\", \"mail.smtp.host\": \"smtp.qq.com\", \"mail.smtp.port\": \"25\", \"mail.imap.fetchsize\": \"819200\", \"mail.imap.partialfetch\": \"false\", \"mail.imaps.partialfetch\": \"false\", \"mail.transport.protocol\": \"imap\"}", Map.class);
        if (map != null) {
            for (String string : map.keySet()) {
                p.setProperty(string, (String) map.get(string));
            }
        }
        return p;
    }

    /**
     * @param session
     * @param GetMailContent
     * @return javax.mail.internet.MimeMessage
     * @Author HuJingYu
     * @Date 15:47 2020-12-08
     * @Description 创建邮件
     */
    public MimeMessage createMixedMail(Session session, String GetMailContent) throws Exception {
        // 创建邮件
        MimeMessage message = new MimeMessage(session);
        message.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
        // 设置邮件的基本信息
        // 设置发件人
        message.setFrom(new InternetAddress(EmailUsername, SentMailName));
        //设置收件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(GetMailAddress));
        message.setSubject(GetSubject);
        message.setText(GetMailContent);
        return message;
    }


    /**
     * @param GetMailContent
     * @return void
     * @Author HuJingYu
     * @Date 15:47 2020-12-08
     * @Description 发送邮件
     */
    public void SendEmail(String GetMailContent) {
        try {
            Properties props = getProperties();
            Authenticator authenticator = new Authenticator() {
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("username");
                    String password = props.getProperty("password");
                    return new javax.mail.PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息，创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = createMixedMail(mailSession, GetMailContent);
            // 最后当然就是发送邮件啦
            Transport.send(message);
        } catch (Exception e) {
            System.out.println("你的邮箱配置有问题(多半是授权码问题)");
            e.printStackTrace();
        }
    }
}
